package com.yucc.girlimages.io;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.db4o.Db4oEmbedded;
import com.db4o.EmbeddedObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;
import com.db4o.query.Query;
import com.yucc.girlimages.io.upyun.FolderItem;

public class Db4oHelper {
	private EmbeddedObjectContainer oc = null;
	private Context context;

	public Db4oHelper(Context ctx) {
		context = ctx;
		oc = db();
	}

	private EmbeddedObjectContainer db() {
		try {
			if (oc == null || oc.ext().isClosed()) {
				oc = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPath(context,"android.yap"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(Db4oHelper.class.getName(), e.toString());
		}
		return oc;
	}

	private EmbeddedConfiguration dbConfig() {
		EmbeddedConfiguration ec = Db4oEmbedded.newConfiguration();
		// Index entries by Id
		// Configure proper activation + update depth
		// TODO
		return ec;
	}

	public String db4oDBFullPath(Context ctx,String name) {
		return ctx.getFilesDir() + "/" + name;
	}

	public void close() {
		if (oc != null) {
			oc.close();
			oc = null;
		}
	}

	public void commit() {
		oc.commit();
	}

	public void rollback() {
		oc.rollback();
	}

	public void deleteDatabase() {
		close();
		new File(db4oDBFullPath(context,"android.yap")).delete();
	}
	
	public void backup(String path) {
		oc.ext().backup(path);
	}

	public void restore(String path) {
		deleteDatabase();
		new File(path).renameTo(new File(db4oDBFullPath(context,"android.yap")));
		new File(path).delete();
	}

	public ArrayList<FolderItem> getFavFolderItems() {
		Query query = db().query() ;
		query.constrain(FolderItem.class) ;
		ObjectSet<FolderItem> set = query.execute(); 
		
		ArrayList<FolderItem> result = null ;
		if(set.size() > 0){
			result = new ArrayList<FolderItem>() ;
		}
		while(set.hasNext()){
			result.add(set.next());
		}
		return result;
	}
	
	public FolderItem getFavFolderItemByName(final String name){
		final ObjectSet<FolderItem> set = db().query(new Predicate<FolderItem>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1231231231L;

			@Override
			public boolean match(FolderItem arg0) {
				if(name.equals(arg0.toString())) return true ;
				return false;
			}
		}) ;
		
		if(set.hasNext()) return set.next() ;
		else return null ;
	}
	
	public void deleteFav() {
		ObjectSet<FolderItem> set = db().query(new Predicate<FolderItem>() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 8109935817933773087L;

			@Override
			public boolean match(FolderItem arg0) {
				// TODO Auto-generated method stub
				return false;
			}
			
		}) ;
		
		while(set.hasNext()){
			db().delete(set.next()) ;
		}
	}
	
	public void saveFavFolderItems(ArrayList<FolderItem> list) {
		int size = list.size() ;
		for(int i=0; i<size; i++){
			saveFavFolderItem(list.get(i)) ;
		}
	}
	
	public void saveFavFolderItem(FolderItem item) {
		db().store(item) ;
	}
}

